<include file="public@header_vue"/>
<script src="https://cdn.jsdelivr.net/npm/vue-ueditor-wrap@latest/lib/vue-ueditor-wrap.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/UEditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/UEditor/ueditor.all.min.js"></script>
</head>
<style>
    .el-pagination {
        margin-top: 20px;
    }


    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .photos {
        width: 120px;
        height: 120px;
        border-radius: 6px;
        overflow: hidden;
        float: left;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
    }

    .photos-options {
        position: absolute;
        top: 0;
        left: 0;
        height: 120px;
        line-height: 120px;
        width: 120px;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.6);
        z-index: 10;
        opacity: 0;
        transition: opacity 0.6s;
    }

    .photos:hover .photos-options {
        opacity: 1;
    }

    .photos-options .el-button {
        font-size: 24px;
        color: white
    }

    .photos img {
        width: 120px;
        height: 120px;
    }

    .photos-uploader {
        width: 120px;
        height: 120px;
        float: left;
    }

    .logo-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .logo-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .demo-color-box {
        position: relative;
        border-radius: 4px;
        padding: 20px;
        width: 100px;
        height: 100px;
        box-sizing: border-box;
        color: #fff;
        font-size: 12px;
    }

    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 120px;
        height: 120px;
        line-height: 120px;
        text-align: center;
    }
</style>
<body>
<div id="app">
    <el-tabs type="border-card">
        <el-tab-pane label="优惠券列表" v-loading="onDataLoading">
            <el-row>
                <el-col :span="24">
                    <el-form :inline="true" :model="form" size="mini">
                        <el-form-item label="日期:">
                            <el-date-picker v-model="form.duration" type="daterange" value-format="yyyy-MM-dd"
                                            :picker-options="durationOptions" range-separator="至"
                                            start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="getData()">查询</el-button>
                            <el-button type="danger" @click="clearForm()">清空</el-button>
                            <el-button type="primary" @click="update(null)">新增优惠券</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="title" label="优惠券标题"></el-table-column>
                <el-table-column prop="coupon_type" label="优惠券类型">
                    <template slot-scope="scope">
                        {{scope.row.coupon_type == 'CASH' ? '满减券': scope.row.coupon_type== 'DISCOUNT' ? '折扣券' : '兑换券'}}
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="优惠券种类">
                    <template slot-scope="scope">
                        {{scope.row.type == 0 ? '普通优惠券' : '直播优惠券'}}
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="优惠券状态">
                    <template slot-scope="scope">
                        {{scope.row.status == 'CARD_STATUS_NOT_VERIFY' ? '待审核' : scope.row.status ==
                        'CARD_STATUS_VERIFY_FAIL' ? '审核失败' : scope.row.status == 'CARD_STATUS_VERIFY_OK' ? '通过审核' :
                        scope.row.status == 'CARD_STATUS_USER_DELETE' ? '卡券被商户删除' : '在公众平台投放过的卡券'}}
                    </template>
                </el-table-column>
                <el-table-column prop="quantity" label="发行数量"></el-table-column>
                <el-table-column prop="card_list_count" label="领取数量"></el-table-column>
                <el-table-column prop="create_time" label="创建时间" min-width="120">
                    <template slot-scope="scope">
                        {{formatDate(scope.row.create_time)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="update(scope.row)">编辑</el-button>
                        <el-button size="mini" type="text" @click="send_card(scope.row.id)">发送优惠券</el-button>
                        <el-button size="mini" type="text"
                                   @click="update_status(scope.row.company_id,scope.row.card_id)">
                            获取微信优惠券状态
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="totalNum" :page-size="10"
                           :current-page.sync="currentPage" @current-change="getData"></el-pagination>
        </el-tab-pane>
        <el-dialog :title="sourceFrom.id == '' ? '新增优惠券' : '修改优惠券'" :visible.sync="dialogVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-form :model="sourceFrom" class="demo-form-inline" :rules="rules" ref="sourceFrom"
                     label-width="200px">
                <el-form-item v-if="{$role_id}!=3" label="选择公司" prop="company_id">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.company_id" placeholder="选择公司">
                            <el-option
                                    v-for="item in companyData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="logo图片" prop="logo_url">
                    <el-col :span="18">
                        <el-upload
                                class="avatar-uploader"
                                action="{:url('api/crm/upload')}"
                                :show-file-list="false"
                                :on-success="handleLogoSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="sourceFrom.logo_url" :src="sourceFrom.logo_url"
                                 class="avatar"/>
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-col>
                </el-form-item>
                <el-form-item label="商户名" prop="brand_name">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.brand_name" placeholder="商户名"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="优惠券标题" prop="title">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.title" placeholder="优惠券标题"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="卡券背景色" prop="color">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.color"></el-input>
                        <el-tag
                                v-for="item in colors"
                                :key="item.name"
                                :hit="false"
                                :color="item.value"
                                style="margin: 0 5px;"
                                @click="select_color(item)"
                                effect="dark">
                            {{ item.name }}
                        </el-tag>
                    </el-col>
                </el-form-item>
                <el-form-item label="优惠券类别" prop="coupon_type">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.coupon_type" placeholder="选择优惠券类别">
                            <el-option
                                    v-for="item in coupon_type_list"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.coupon_type == 'CASH'" label="起用金额" prop="least_cost">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.least_cost" placeholder="起用金额"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.coupon_type == 'CASH'" label="减免金额" prop="reduce_cost">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.reduce_cost" placeholder="减免金额"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.coupon_type == 'DISCOUNT'" label="打折额度" prop="discount">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.discount" placeholder="打折额度,填30就是七折"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.coupon_type == 'GIFT'" label="兑换内容" prop="gift">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.gift" placeholder="兑换内容" type="textarea"
                                  maxlength="1024"
                                  show-word-limit></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="优惠券标题" prop="title">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.title" placeholder="优惠券标题"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="优惠券种类" prop="type">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.type" placeholder="选择优惠券种类">
                            <el-option
                                    v-for="item in type_list"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="Code展示类型" prop="code_type">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.code_type" placeholder="选择Code展示类型">
                            <el-option
                                    v-for="item in code_type"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="使用范围" prop="use_condition">
                    <el-col :span="18">
                        <el-tag
                                v-for="tag in sourceFrom.use_condition"
                                :key="tag.id"
                                closable
                                @close="handleClose(tag)"
                                type="info">
                            {{tag.goods_name}}
                        </el-tag>
                        <el-button type="primary" @click="select_goods">选择可使用商品</el-button>
                    </el-col>
                </el-form-item>
                <el-form-item label="使用日期" prop="date_info_type">
                    <el-col :span="18">
                        <el-select v-model="sourceFrom.date_info_type" placeholder="选择使用日期">
                            <el-option
                                    v-for="item in date_type"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.date_info_type == 'DATE_TYPE_FIX_TIME_RANGE'" label="有效期"
                              prop="activity_time">
                    <el-col :span="18">
                        <el-date-picker v-model="sourceFrom.activity_time" type="daterange" value-format="yyyy-MM-dd"
                                        :picker-options="durationOptions" range-separator="至"
                                        start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.date_info_type == 'DATE_TYPE_FIX_TERM'" label="多少天后生效"
                              prop="fixed_begin_term">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.fixed_begin_term" type="number" placeholder="多少天后生效"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.date_info_type == 'DATE_TYPE_FIX_TERM'" label="多少内生效"
                              prop="fixed_term">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.fixed_term" type="number" placeholder="多少内生效"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item v-if="sourceFrom.date_info_type == 'DATE_TYPE_FIX_TERM'" label="优惠券过期时间"
                              prop="end_timestamp">
                    <el-col :span="18">
                        <el-date-picker
                                v-model="sourceFrom.end_timestamp"
                                type="date"
                                placeholder="选择优惠券过期时间">
                        </el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="发行数量" prop="quantity">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.quantity" type="number" placeholder="发行数量"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="是否可以领取" prop="is_receive">
                    <el-col :span="18">
                        <el-switch
                                v-model="sourceFrom.is_receive"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                :active-value="1"
                                :inactive-value="0">
                        </el-switch>
                    </el-col>
                </el-form-item>
                <el-form-item label="限领张数" prop="get_limit">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.get_limit" type="number" placeholder="限领张数"></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="使用提醒" prop="notice">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.notice" placeholder="优惠券说明" maxlength="16"
                                  show-word-limit></el-input>
                    </el-col>
                </el-form-item>
                <el-form-item label="使用说明" prop="description">
                    <el-col :span="18">
                        <el-input v-model="sourceFrom.description" placeholder="优惠券说明" type="textarea"
                                  maxlength="1024"
                                  show-word-limit></el-input>
                    </el-col>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button v-if="sourceFrom.id === ''" type="primary" @click="submit('sourceFrom')">新增</el-button>
            <el-button v-else type="primary" @click="submit('sourceFrom')">修改</el-button>
        </span>
        </el-dialog>
        <el-dialog title="选择商品" :visible.sync="dialogGoodsVisible" width="60%"
                   v-loading.fullscreen="dialogLoading">
            <el-tag
                    v-for="tag in sourceFrom.use_condition"
                    :key="tag.id"
                    closable
                    @close="handleClose(tag)"
                    type="info">
                {{tag.goods_name}}
            </el-tag>
            <el-table :data="goodsData" style="width: 100%">
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="goods_name" label="商品标题"></el-table-column>
                <el-table-column prop="cover_img" label="商品图片">
                    <template slot-scope="scope">
                        <img :src="scope.row.goods_img[0]" alt="" style="width: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="select(scope.row)">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="goodsNum" :page-size="10"
                           :current-page.sync="currentGoodsPage" @current-change="init_goods"></el-pagination>
        </el-dialog>
        <el-dialog title="选择用户" :visible.sync="dialogUserVisible" width="80%"
                   v-loading.fullscreen="dialogLoading">
            <el-tag
                    v-for="user in user_list"
                    :key="user.id"
                    closable
                    @close="handleUserClose(user)"
                    type="info">
                {{user.nickname}}
            </el-tag>
            <div>
                <el-button @click="toggleSelection()">取消选择</el-button>
                <el-button @click="add_user_list()">加入发送列表</el-button>
                <el-button @click="do_send_card()">发送优惠券</el-button>
            </div>
            <el-table :data="userData" style="width: 100%"
                      ref="multipleTable"
                      tooltip-effect="dark"
                      @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="nickname" label="用户昵称"></el-table-column>
                <el-table-column prop="" label="用户头像">
                    <template slot-scope="scope">
                        <img :src="scope.row.headimgurl" alt="" style="width: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" @click="selectUser(scope.row)">选择</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination background layout="total,prev, pager, next" :total="userNum" :page-size="10"
                           :current-page.sync="currentUserPage" @current-change="init_user"></el-pagination>
        </el-dialog>
    </el-tabs>
</div>
</body>
<script>
    Vue.component('vue-ueditor-wrap', VueUeditorWrap)
    var app = new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [],
                companyData: [],
                goodsData: [],
                user_list: [],
                openid_list: [],
                userData: [],
                goodsNum: 0,
                userNum: 0,
                currentUserPage: 1,
                currentGoodsPage: 1,
                dialogGoodsVisible: false,
                dialogUserVisible: false,
                form: {
                    name: '',
                },
                colors: [
                    {
                        "name": "Color010",
                        "value": "#63b359"
                    },
                    {
                        "name": "Color020",
                        "value": "#2c9f67"
                    },
                    {
                        "name": "Color030",
                        "value": "#509fc9"
                    },
                    {
                        "name": "Color040",
                        "value": "#5885cf"
                    },
                    {
                        "name": "Color050",
                        "value": "#9062c0"
                    },
                    {
                        "name": "Color060",
                        "value": "#d09a45"
                    },
                    {
                        "name": "Color070",
                        "value": "#e4b138"
                    },
                    {
                        "name": "Color080",
                        "value": "#ee903c"
                    },
                    {
                        "name": "Color081",
                        "value": "#f08500"
                    },
                    {
                        "name": "Color082",
                        "value": "#a9d92d"
                    },
                    {
                        "name": "Color090",
                        "value": "#dd6549"
                    },
                    {
                        "name": "Color100",
                        "value": "#cc463d"
                    },
                    {
                        "name": "Color101",
                        "value": "#cf3e36"
                    },
                    {
                        "name": "Color102",
                        "value": "#5E6671"
                    },
                    {
                        "name": "Color103",
                        "value": "#026842"
                    }
                ],
                myConfig: {
                    // 编辑器不自动被内容撑高
                    autoHeightEnabled: false,
                    // 初始容器高度
                    initialFrameHeight: 400,
                    zIndex: 3000,
                    // 初始容器宽度
                    initialFrameWidth: '90%',
                    // 上传文件接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
                    serverUrl: 'http://ml.0513app.cn/static/UEditor/php/controller.php',
                    // UEditor 资源文件的存放路径，如果你使用的是 vue-cli 生成的项目，通常不需要设置该选项，vue-ueditor-wrap 会自动处理常见的情况，如果需要特殊配置，参考下方的常见问题2
                    UEDITOR_HOME_URL: '/static/UEditor/',
                },
                totalNum: 0,
                currentPage: 1,
                dialogLoading: false,
                onDataLoading: false,
                durationOptions: {
                    shortcuts: [{
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            end.setTime(end.getTime() + 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                sourceFrom: {
                    id: '',
                    name: '',
                    money: '',
                    activity_time: '',
                    sign_time: '',
                    give_points: '',
                    imgs: [],
                },
                dialogVisible: false,
                coupon_type_list: [
                    {id: 0, name: '折扣券', value: 'DISCOUNT'},
                    {id: 1, name: '满减券', value: 'CASH'},
                    {id: 2, name: '兑换券', value: 'GIFT'}
                ],
                code_type: [
                    {id: 0, name: '文本', value: 'CODE_TYPE_TEX'},
                    {id: 1, name: '一维码', value: 'CODE_TYPE_BARCODE'},
                    {id: 2, name: '二维码', value: 'CODE_TYPE_QRCODE'},
                    {id: 3, name: '仅显示二维码', value: 'CODE_TYPE_ONLY_QRCODE'},
                    {id: 4, name: '仅显示一维码', value: 'CODE_TYPE_ONLY_BARCODE'},
                    {id: 5, name: '不显示任何码型', value: 'CODE_TYPE_NONE'},
                ],
                multipleSelection: [],
                date_type: [
                    {id: 1, 'name': '有效日期', value: 'DATE_TYPE_FIX_TIME_RANGE'},
                    {id: 2, 'name': '领取后多少天内有效', value: 'DATE_TYPE_FIX_TERM'},
                ],
                current_coupon_id: 0,
                type_list: [
                    {id: 0, name: '普通优惠券'},
                    {id: 1, name: '直播优惠券'},
                ],
                rules: {
                    title: [{required: true, message: '请输入优惠券标题', trigger: 'blur'}],
                    company_id: [{required: true, message: '请选择公司', trigger: 'blur'}],
                    color: [{required: true, message: '请选择卡券背景颜色', trigger: 'blur'}],
                    coupon_type: [{required: true, message: '请选择优惠券类别', trigger: 'blur'}],
                    type: [{required: true, message: '请选择优惠券种类', trigger: 'blur'}],
                    code_type: [{required: true, message: '请选择展示code类型', trigger: 'blur'}],
                    use_condition: [{required: true, message: '请选择使用范围', trigger: 'blur'}],
                    date_info_type: [{required: true, message: '请选择使用日期类型', trigger: 'blur'}],
                    activity_time: [{required: true, message: '请选择有效日期', trigger: 'blur'}],
                    end_timestamp: [{required: true, message: '请选择优惠券过期时间', trigger: 'blur'}],
                    fixed_term: [{required: true, message: '请输入正确数字', trigger: 'blur'}],
                    fixed_begin_term: [{required: true, message: '请输入正确数字', trigger: 'blur'}],
                    quantity: [{required: true, message: '请输入发行数量', trigger: 'blur'}],
                    notice: [{required: true, message: '请输入优惠券使用提醒', trigger: 'blur'}],
                    description: [{required: true, message: '请输入优惠券使用说明', trigger: 'blur'}],
                    reduce_cost: [{required: true, message: '请输入减免金额', trigger: 'blur'}],
                    least_cost: [{required: true, message: '请输入满足金额', trigger: 'blur'}],
                    discount: [{required: true, message: '请输入优惠额度', trigger: 'blur'}],
                    gift: [{required: true, message: '请输入兑换券内容', trigger: 'blur'}],
                },
                headers: {'X-Requested-With': 'XMLHttpRequest'},
            }
        },
        created() {
            this.getCompanyData();
            this.getData();
            this.init_goods();
            this.init_user();
        },
        methods: {
            clearForm() {
                this.form = {
                    name: '',
                }
            },
            add_user_list() {
                let array = this.multipleSelection;
                let _this = this;
                array.forEach(function (x) {
                    console.log(_this.openid_list.indexOf(x.openid))
                    if (_this.openid_list.indexOf(x.openid) == -1) {
                        _this.openid_list.push(x.openid);
                        _this.user_list.push(x);
                    }
                })
                console.log(_this.openid_list)
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val;
            },
            init_user() {
                let _this = this;
                let params = {page: this.currentUserPage};
                this.onDataLoading = true;
                axios.get("{:url('admin/goods/user_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.userData = response.data.data.data;
                        _this.userNum = response.data.data.total;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            do_send_card() {
                let _this = this;
                console.log(this.openid_list)
                if (this.openid_list.length <= 1) {
                    _this.$message({
                        message: '请选择多个用户',
                        type: 'error'
                    });
                    return;
                }
                let params = {id: this.current_coupon_id, openid_list: this.openid_list};

                axios.post("{:url('admin/coupon/send_card')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.$message({
                            message: response.data.msg,
                            type: 'success'
                        });
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            send_card(id) {
                this.dialogUserVisible = true;
                this.current_coupon_id = id;

            },
            update_status(company_id, card_id) {
                let params = {company_id: company_id, card_id: card_id};
                let _this = this;
                axios.get("{:url('admin/coupon/update_status')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.$message({
                            message: response.data.msg,
                            type: 'success'
                        });
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            select(row) {
                let array = this.sourceFrom.use_condition;
                let _this = this;
                if (array.length > 0) {
                    let is_exits = true;
                    array.forEach(function (x, index) {
                        if (x.id == row.id) {
                            is_exits = false
                            return;
                        }
                    });
                    if (is_exits) {
                        _this.sourceFrom.use_condition.push(row)
                    } else {
                        this.$message({
                            message: '已存在，请勿重复添加',
                            type: 'warning'
                        });
                    }
                } else {
                    this.sourceFrom.use_condition.push(row)
                }

            },
            selectUser(row) {
                let array = this.openid_list;
                let _this = this;
                console.log(array)
                if (array.indexOf(row.openid) == -1) {
                    _this.openid_list.push(row.openid)
                    _this.user_list.push(row)
                } else {
                    _this.$message({
                        message: '已存在，请勿重复添加',
                        type: 'warning'
                    });
                }

            },
            handleClose(tag) {
                this.sourceFrom.use_condition.splice(this.sourceFrom.use_condition.indexOf(tag), 1);
            },
            handleUserClose(user) {
                this.user_list.splice(this.user_list.indexOf(user), 1);
            },
            init_goods() {
                let _this = this;
                let params = {page: this.currentGoodsPage, is_on_sale: 1};
                this.onDataLoading = true;
                if (this.form.duration !== '') {
                    params.duration = this.form.duration;
                }
                axios.get("{:url('admin/goods/goods_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.goodsData = response.data.data.data;
                        _this.goodsNum = response.data.data.total;
                        console.log(_this.goodsNum)
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            select_goods() {
                this.dialogGoodsVisible = true;
            },
            select_color(name) {
                this.sourceFrom.color = name.name;
            },
            getData(page = 1) {
                let _this = this;
                let params = {page: page};
                this.onDataLoading = true;
                if (this.form.duration !== '') {
                    params.duration = this.form.duration;
                }
                axios.get("{:url('admin/coupon/index')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                    params: params
                }).then(function (response) {
                    console.log(response.data.data);
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.tableData = response.data.data.data;
                        _this.totalNum = response.data.data.total;
                        console.log(_this.totalNum)
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            getCompanyData() {
                let _this = this;
                axios.get("{:url('admin/company/get_company_list')}", {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    },
                }).then(function (response) {
                    _this.onDataLoading = false;
                    if (response.data.code === 1) {
                        _this.companyData = response.data.data;
                    } else {
                        _this.$message({
                            message: response.data.msg,
                            type: 'warning'
                        });
                    }
                }).catch(function (error) {
                    _this.onDataLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            handleLogoSuccess(res, file) {
                this.sourceFrom.logo_url = res.url;
            },
            handleRemove(file, fileList) {
                let index = this.sourceFrom.imgs.indexOf(file);
                this.sourceFrom.imgs.splice(index, 1);
            },
            handleintroimgSuccess(res, file) {
                if (res.code === 1) {
                    this.sourceFrom.imgs.push(res.url);
                } else {
                    this.$message.error(res.msg)
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || 'image/png';

                const isLt2M = file.size / 1024 / 1024 < 10;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式或者PNG格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            change_status(field, value, id) {
                let _this = this;
                this.dialogLoading = true;
                let params = {field: field, value: value, id: id};
                axios.post("{:url('admin/activity/change_status')}", params, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        _this.$message.success('修改完成');
                        _this.getData(_this.currentPage);
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                })
            },
            update(data) {
                let _this = this;
                if (data) {
                    _this.sourceFrom = JSON.parse(JSON.stringify(data));
                } else {
                    _this.sourceFrom = {
                        id: '',
                        title: '',
                        coupon_type: '',
                        type: '',
                        code_type: '',
                        use_condition: [],
                        date_info_type: '',
                        activity_time: '',
                        end_timestamp: '',
                        fixed_begin_term: '',
                        fixed_term: '',
                        color: '',
                        get_limit: '',
                        quantity: '',
                        notice: '',
                        description: '',
                        logo_url: '',
                        is_receive: 0
                    }
                }
                _this.dialogVisible = true;
                _this.dialogLoading = false;
            },
            submit(form) {
                let _this = this;
                _this.$refs[form].validate((valid) => {
                    if (valid) {
                        axios.post("{:url('admin/coupon/update')}", this.sourceFrom, {
                            headers: {
                                'X-Requested-With': 'XMLHttpRequest',
                            }
                        }).then(function (response) {
                            _this.dialogLoading = false;
                            if (response.data.code === 1) {
                                if (_this.sourceFrom.id === '') {
                                    _this.$message.success('新增完成');
                                } else {
                                    _this.$message.success('修改成功');
                                }
                                _this.getData();
                                _this.dialogVisible = false;
                            } else {
                                _this.$message.warning(response.data.msg);
                            }
                        }).catch(function (error) {
                            _this.dialogLoading = false;
                            _this.$message.error(error.toString())
                        })
                    } else {
                        _this.dialogLoading = false;
                        return false;
                    }
                });
            },
            delete_activity(id) {
                let _this = this;
                axios.post("{:url('admin/activity/delete')}", {id: id}, {
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest',
                    }
                }).then(function (response) {
                    _this.dialogLoading = false;
                    if (response.data.code === 1) {
                        if (_this.sourceFrom.id === '') {
                            _this.$message.success('新增完成');
                        } else {
                            _this.$message.success('修改成功');
                        }
                        _this.getData(_this.currentPage);
                        _this.dialogVisible = false;
                    } else {
                        _this.$message.warning(response.data.msg);
                    }
                }).catch(function (error) {
                    _this.dialogLoading = false;
                    _this.$message.error(error.toString())
                })
            },
            formatDate(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp * 1000);
                    return date.getFullYear() + '-' + this.prefix(date.getMonth() + 1) + '-' + this.prefix(date.getDate()) + ' ' + this.prefix(date.getHours()) + ':' + this.prefix(date.getMinutes()) + ':' + this.prefix(date.getSeconds());
                } else {
                    return '';
                }
            },
            prefix(num) {
                if (num < 10) {
                    return '0' + num;
                }
                return num;
            }
        }
    });
</script>
